package servlets;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class xsltUpload
 */
public class xsltUpload extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public xsltUpload() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String fileName = request.getParameter("xsltName");
		String fileContent = request.getParameter("xsltContent");
		File file = new File(this.getServletContext().getRealPath("xmlTable"),fileName);
		System.out.println();
		if(!file.exists()){
			FileWriter fw = new FileWriter(file);
	        PrintWriter pw = new PrintWriter(fw, true);
	        pw.print(fileContent);
	        pw.flush();
	        pw.close();
	        out.print("<firefoxsucks>success</firefoxsucks>");
		}else{
			out.print("<firefoxsucks>failure</firefoxsucks>");
		}
	}

}
